<?php
/**
 * Created by PhpStorm.
 * User: zyark
 * Date: 2017/8/15
 * Time: 10:25
 */

namespace Admin\Controller;


use Admin\Logic\RoomLogic;
use Admin\Model\Page;
use Common\Utils\Constants;

class RoomController extends BaseController
{
    public function __construct($baseModel)
    {
        parent::__construct(D('Room'));
        $this->bcItemPush('房间列表', U('Room/index'));
    }

    public function index(){
        $RoomModel = D('Room');
        //构建查询条件
        $pagesize = 20;
        $user_id = I('user_id');
        $region = I('region');
        $systems_name = I('systems_name');
        $model = I('model');
        $integral_interval = I('integral_interval');
        $status = I('status');
        $money_type = I('money_type');
        $id = I('id');
        $pagenumber = I('pagenum', '');
        $startTime = I('startTime', '');
        $endTime = I('endTime', '');
        if (empty($pagenumber) || $pagenumber <= 0)
            $pagenumber = 1;
        if ($user_id != '') {
            $map['t_room.user_id'] = $user_id;
        }
        if ($id != '') {
            $map['t_room.id'] = $id;
        }
        if ($region != '' && $region != '-1') {
            $map['t_room.region'] = $region;
        }
        if ($systems_name != '' && $systems_name != '-1') {
            $map['t_room.systems_name'] = $systems_name;
        }
        if ($model != '' && $model != '-1') {
            $map['t_room.model'] = $model;
        }
        if ($integral_interval != '' && $integral_interval != '-1') {
            $map['t_room.integral_interval'] = $integral_interval;
        }
        if ($status != '' && $status != '-1') {
            $map['t_room.status'] = $status;
        }
        if ($money_type != '' && $money_type != '-1') {
            $map['t_room.money_type'] = $money_type;
        }
        if ($startTime != null && $startTime != '' && $endTime != null && $endTime != '') {
            $map['t_room.create_time'] = array(array('gt', strtotime($startTime)), array('lt', strtotime($endTime)));
        }
        //查询
        $startno = ($pagenumber - 1) * $pagesize;
        $limit = $startno . ',' . $pagesize;//每页的数据数和内容$limit
        $list = $RoomModel->where($map)->order('id desc')->limit($limit)
            ->field('t_room.*')->select();
        $recordcount = $RoomModel->where($map)->count();
        //封装分页对象
        $page = new Page($pagenumber, $recordcount, $pagesize);
        $page = (array)$page;//转换为数组

        //显示数据
        $this->assign('startTime', $startTime);
        $this->assign('endTime', $endTime);
        $this->assign('page', $page);
        $this->assign('user_id', $user_id);
        $this->assign('id', $id);
        $this->assign('region', $region);
        $this->assign('systems_name', $systems_name);
        $this->assign('model', $model);
        $this->assign('integral_interval', $integral_interval);
        $this->assign('status', $status);
        $this->assign('money_type', $money_type);
        $this->assign('pagenumber', $pagenumber);
        $this->assign('list', $list);
        $this->assign('recordcount', $recordcount);
        $this->display();
    }

    /**
     * ajax解散房间
     */
    public function dissolve(){
        $room_id = I('room_id');
        $remark = I('remark');
        $is_poundage = I('is_poundage');
        $payer = I('payer');
        if($is_poundage !=1 && $is_poundage !=0){
            $this->ajaxReturn(array(Constants::SUCCESS=>false,Constants::MESSAGE=>'请选择是否需要手续费！'));
        }
        if($is_poundage == 1){
            if($payer !=0 && $payer !=1){
                $this->ajaxReturn(array(Constants::SUCCESS=>false,Constants::MESSAGE=>'请选择支付者！'));
            }
        }
        $logic = new RoomLogic();
        $status = $logic->dissolve($room_id,$remark,$is_poundage,$payer);
        $success = false;
        $message = '';
        switch ($status){
            case 0:
                $message = '解散失败！';
                break;
            case 1:
                $success = true;
                $message = '解散成功！';
                break;
            case 2:
                $message = '该房间已经解散！';
                break;
            case 3:
                $message = '该房间已经打擂结束！';
                break;
            case 4:
                $message = '该擂主钻石不足!';
                break;
            case 5:
                $message = '该擂主金币不足！';
                break;
            case 6:
                $message = '该房间不存在打擂者!';
                break;
        }
        $this->ajaxReturn(array(Constants::SUCCESS=>$success,Constants::MESSAGE=>$message));
    }

    public function dissolveAll(){
        $logic = new RoomLogic();
        $status = $logic->dissolveAll();
        $success = false;
        $message = '';
        switch ($status){
            case 0:
                $message = '解散失败！';
                break;
            case 1:
                $success = true;
                $message = '解散成功！';
                break;
            case 2:
                $message = '系统配置的房间超时时间有误！';
                break;
        }
        $this->ajaxReturn(array(Constants::SUCCESS=>$success,Constants::MESSAGE=>$message));
    }

//    public function timeTest(){
//        $roomList = D('Room')->where(array('status'=>Constants::ROOM_STATUS_WAIT))->select();
//        $model = M();
//        $config = $model->table('t_config')->where(array('name'=>Constants::CONFIG_DISBANDED_ROOM_TIME))->find();
//        $time = $config['value'];
//        if(!is_numeric($time) || $time<0) {
//            echo 'error';
//            return;
//        }
//        $now = time();
//        $list = array();
//        foreach ($roomList as $key=>$value){
//            $time_off =intval(($now - $value['create_time'] ) / 60);
//            var_dump($time_off);
////            var_dump($value['create_time'].'   id:   '.$value['id']);
//            if($time_off < $time){
//                continue;
//            }
//            $list[] = $value;
//        }
//        foreach ($list as $key=>$value){
//            var_dump($value['create_time'].'   id:   '.$value['id']);
//        }
//    }

//    public function saveRoomTest(){
//        $userList = D('User')->select();
//        foreach ($userList as $key=> $value){
//            for ($i=0;$i<4;$i++){
//                $room['region'] = $i % 2;
//                $room['systems_name'] = $i % 2;
//                $room['model'] = $i %3;
//                $room['integral_interval'] = $i % 2;
//                $room['status'] = $i % 4;
//                $room['integral'] = get_rand_number(2);
//                $room['create_time'] = time();
//                $room['declaration'] = get_rand_code(20);
//                $room['user_id'] = $value['id'];
//                $user_id = $userList[$i]['id'];
//                $room['opponent_id'] = $user_id;
//                D('Room')->add($room);
//            }
//        }
//    }
}